import { getFormData, getFormSaveData } from '@/formgen/src/utils/db'

export default {
  data() {
    return {
      confirmTitle: '',
      showConfirm: false,
      // 0: 不恢复 1: 恢复未提交的数据 2: 使用上次提交的数据
      restoreType: 0
    }
  },
  watch: {},
  created() {},
  methods: {
    /**
     * 恢复保存未提交的数据
     */
    restoreNotSaveData() {
      // 是否有上次未提交数据
      const formModel = getFormData(this.formConf.formKey)
      if (this.formConf.setting && this.formConf.setting.saveNotSubmitStatus && formModel) {
        this.confirmTitle = '恢复未提交的数据？'
        this.showConfirm = true
        this.restoreType = 1
      }
    },
    /**
     * 使用上次提交的数据
     */
    restoreLastSubmitData() {
      const formModel = getFormSaveData(this.formConf.formKey)
      // 带入已提交的数据
      if (this.formConf.setting && this.formConf.setting.saveSubmitStatus && formModel) {
        this.confirmTitle = '使用上次提交的数据？'
        this.showConfirm = true
        this.restoreType = 2
      }
    },
    restoreData() {
      let formModel = null
      switch (this.restoreType) {
        case 1:
          formModel = getFormData(this.formConf.formKey)
          break
        case 2:
          formModel = getFormSaveData(this.formConf.formKey)
          break
      }
      if (this.restoreType) {
        this.pageFormModel = formModel || {}
        this.parserKey = +new Date()
        this.showConfirm = false
      }
    },
    /**
     * 更新逻辑显示
     */
    updateLogicShow() {
      this.$nextTick(() => {
        // 处理逻辑显示
        for (let key of Object.keys(this.pageFormModel)) {
          if (this.pageFormModel[key]) {
            this.$refs.pageContent.logicShowHandle(this.pageFormModel[key], key, {})
          }
        }
      })
    }
  }
}
